package org.karelun.gef.layers;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LayeredPane;
import org.eclipse.draw2d.ScalableFigure;
import org.eclipse.draw2d.ScalableFreeformLayeredPane;
import org.eclipse.draw2d.Viewport;
import org.eclipse.gef.editparts.FreeformGraphicalRootEditPart;
import org.eclipse.gef.editparts.GuideLayer;
import org.eclipse.gef.editparts.ZoomManager;

public class CustomScalableFreeformRootEditPart extends
		FreeformGraphicalRootEditPart {

	private class FeedbackLayer extends FreeformLayer {

		public FeedbackLayer() {
//			setEnabled(false);
//			setBackgroundColor(ColorConstants.blue);
			setForegroundColor(ColorConstants.white);
		}
		
//		@Override
//		public void paint(Graphics graphics) {
//			super.paint(graphics);
//			graphics.setBackgroundColor(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
//		}
//		
//		@Override
//		protected void paintFigure(Graphics graphics) {
//			super.paintFigure(graphics);
//			Color color = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
//			graphics.setBackgroundColor(color);
//			graphics.setForegroundColor(color);
//		}
		
	}

	private ScalableFreeformLayeredPane scaledLayers;

	private ZoomManager zoomManager;

	public CustomScalableFreeformRootEditPart() {
		zoomManager = new ZoomManager((ScalableFigure) getScaledLayers(),
				(Viewport) getFigure());
	}

	protected void createLayers(LayeredPane layeredPane) {
		layeredPane.add(getScaledLayers(), SCALABLE_LAYERS);
		layeredPane.add(new FreeformLayer(), HANDLE_LAYER);
		layeredPane.add(new FeedbackLayer(), FEEDBACK_LAYER);
		layeredPane.add(new GuideLayer(), GUIDE_LAYER);
	}

	protected ScalableFreeformLayeredPane createScaledLayers() {
		ScalableFreeformLayeredPane layers = new ScalableFreeformLayeredPane();
		layers.add(createGridLayer(), GRID_LAYER);
		layers.add(getPrintableLayers(), PRINTABLE_LAYERS);
		layers.add(new FeedbackLayer(), SCALED_FEEDBACK_LAYER);
		return layers;
	}

	public IFigure getLayer(Object key) {
		IFigure layer = scaledLayers.getLayer(key);
		if (layer != null)
			return layer;
		return super.getLayer(key);
	}

	protected LayeredPane getScaledLayers() {
		if (scaledLayers == null)
			scaledLayers = createScaledLayers();
		return scaledLayers;
	}

	public ZoomManager getZoomManager() {
		return zoomManager;
	}

	protected void register() {
		super.register();
		getViewer().setProperty(ZoomManager.class.toString(), getZoomManager());
	}

	protected void unregister() {
		super.unregister();
		getViewer().setProperty(ZoomManager.class.toString(), null);
	}

}
